// scripts/parse-coverage.js
import fs from 'fs';
import path from 'path';

const coverageFile = new URL('../coverage/coverage-final.json', import.meta.url);
const data = JSON.parse(await fs.promises.readFile(coverageFile, 'utf8'));

const result = [];

for (const file of Object.values(data)) {
  const { path: filePath, s: statements, statementMap } = file;
  const className = path.basename(filePath, path.extname(filePath));

  // 获取被覆盖的语句索引
  const coveredStatementIds = Object.entries(statements)
    .filter(([_, count]) => count > 0)
    .map(([id]) => parseInt(id, 10));

  // 映射到实际行号
  const coveredLines = coveredStatementIds
    .map(id => statementMap[id].start.line)
    .sort((a, b) => a - b);

  result.push({
    filePath,
    className,
    coveredLines
  });
}

console.log(JSON.stringify(result, null, 2));